import { createRouter, createWebHistory } from 'vue-router'
import { useLoginEmpStore } from '@/stores/loginEmp'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: '主布局',
      component: () => import('../views/layout/index.vue'),
      redirect: '/control',
      children: [
        { path: 'control', name: '控制台', component: () => import('../views/Control Panel/index.vue') },
        {
          path: 'todo',
          name: '待办中心',
          children: [
            { path: 'invoice', name: '开票申请审核', component: () => import('../views/Todo Center/Invoice Application Review/index.vue') },
            { path: 'complaint', name: '投诉举报处理', component: () => import('../views/Todo Center/Complaint Handling/index.vue') },
            { path: 'feedback', name: '意见反馈处理', component: () => import('../views/Todo Center/Feedback Handling/index.vue') }
          ]
        },
        { path: 'marketing', name: '营销工具', component: () => import('../views/Marketing Tools/index.vue') },
        { path: 'event', name: '活动管理', component: () => import('../views/Event Management/index.vue') },
        { path: 'enterprise', name: '企业管理', component: () => import('../views/Enterprise Management/index.vue') },
        { path: 'user', name: '用户管理', component: () => import('../views/User Management/index.vue') },
        { path: 'fund', name: '资金明细', component: () => import('../views/Fund Details/index.vue') },
        { path: 'invoice', name: '发票管理', component: () => import('../views/Invoice Management/index.vue') },
        {
          path: 'platform',
          name: '平台管理',
          children: [
            { path: 'event-admin', name: '活动管理员管理', component: () => import('../views/Platform Management/Event Administrator Management/index.vue') },
            { path: 'customer-service', name: '客服专员管理', component: () => import('../views/Platform Management/Customer Service Specialist Management/index.vue') }
          ]
        },
        {
          path: 'account',
          name: '账号信息',
          children: [
            { path: 'personal', name: '个人账号信息', component: () => import('../views/Account Center/Personal Account Information/index.vue') },
            { path: 'permission', name: '账号权限管理', component: () => import('../views/Account Center/Account Permission Management/index.vue') }
          ]
        },
        {
          path: 'record',
          name: '记录中心',
          children: [
            { path: 'log', name: '登录日志', component: () => import('../views/Record Center/Log Record/index.vue') },
            { path: 'operation', name: '操作日志', component: () => import('../views/Record Center/Operation Log/index.vue') }
          ]
        },
        { path: 'feedback', name: '意见反馈', component: () => import('../views/Feedback/index.vue') },
        { path: 'CompiaintsReport', name: '投诉举报', component: () => import('../views/CompiaintsReport/index.vue') }
      ]
    },
    {
      path: '/login',
      name: '登录',
      component: () => import('../views/Login/index.vue')
    }
  ]
})

// 恢复路由守卫，未登录跳转到登录页，已登录访问/login自动跳转到首页
router.beforeEach((to, from, next) => {
  console.log('🛡️ 路由守卫触发:', { to: to.path, from: from.path })

  const loginStore = useLoginEmpStore()
  const loginInfo = loginStore.getLoginEmp()

  console.log('🔍 当前登录信息:', loginInfo)
  console.log('🔑 Token 存在:', !!loginInfo?.token)

  if (to.path !== '/login' && !loginInfo?.token) {
    console.log('🚫 未登录，重定向到登录页')
    // 如果用户未登录且访问的不是登录页，则重定向到登录页
    next('/login')
  } else if (to.path === '/login' && loginInfo?.token) {
    console.log('✅ 已登录，重定向到首页')
    // 如果用户已登录且访问的是登录页，则重定向到首页
    next(from.path !== '/login' ? from.path : '/')
  } else {
    console.log('✅ 允许访问:', to.path)
    next()
  }
})

export default router

